/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"

/** @addtogroup STM32F0xx_StdPeriph_Examples
  * @{
  */

/** @addtogroup IOToggle
  * @{
  */



/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef        GPIO_InitStructure;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  IOToggle program.
  * @param  None
  * @retval None
  */
void GpioInit(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */

  /* GPIOC Periph clock enable */
  //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  RCC->AHBENR |=(RCC_AHBPeriph_GPIOB+RCC_AHBPeriph_GPIOA+RCC_AHBPeriph_DMA1);// RCC_AHBPeriph;

  /* Configure PC8 and PC9 in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIOA->BRR=(0x8000+0x4000);
  /* To achieve GPIO toggling maximum frequency, the following  sequence is mandatory. 
     You can monitor PC8 and PC9 on the scope to measure the output signal. 
     If you need to fine tune this frequency, you can add more GPIO set/reset 
     cycles to minimize more the infinite loop timing.
     This code needs to be compiled with high speed optimization option.  */

}

uint8_t ReadAddr()
{
      uint8_t res=0;
      uint32_t temp;
      uint32_t i;
      
	  GPIOB->BRR =  0x6000;
    
      GPIOA->MODER  &= ~((GPIO_MODER_MODER0 << (14 * 2))
                            +(GPIO_MODER_MODER0<<(13*2)));
      GPIOA->MODER |= (GPIO_Mode_OUT<<(14*2));
      res=GPIOA->IDR>>4;
      GPIOA->MODER  &= ~((GPIO_MODER_MODER0 << (14 * 2))
                            +(GPIO_MODER_MODER0<<(13*2)));
      GPIOA->MODER |= (GPIO_Mode_OUT<<(14*2));
      for(i=0;i<1000;i++)
      {
    	 asm("NOP");
      }
      temp=GPIOA->IDR;
      res=temp>>4;
      res&=0x0f;
     
      GPIOA->MODER  &= ~((GPIO_MODER_MODER0 << (14 * 2))
                            +(GPIO_MODER_MODER0<<(13*2)));
      GPIOA->MODER |= (GPIO_Mode_OUT<<(13*2));
      GPIOA->MODER  &= ~((GPIO_MODER_MODER0 << (14 * 2))
                            +(GPIO_MODER_MODER0<<(13*2)));
      GPIOA->MODER |= (GPIO_Mode_OUT<<(13*2));
      for(i=0;i<1000;i++)
      {
    	  asm("NOP");
      }
      temp=GPIOA->IDR;
      res+=(temp&0xf0);
      res=~res;
      return res;

}

void SetWorkLedOn(void)
{
	 GPIOB->BRR =  2;

}
void SetWorkLedOff(void)
{
	 GPIOB->BSRR = 2;
}

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
